﻿using System.IO;

using NUnit.Framework;
using Ninject;

using System.Net.DnsToolkit.Bastard.PacketGenerator;
using System.Net.DnsToolkit.Configuration;
using System.Net.DnsToolkit.Packet;
using System.Net.DnsToolkit.Serializer;
using System.Net.DnsToolkit.Util;
using System.Net.DnsToolkit.TestUnit.Helper;

namespace System.Net.DnsToolkit.TestUnit.Bastard.PacketGenerator {
    [TestFixture]
    public class DomainPointerSingleLoopTest {

        [Test]
        public void TestGetBytes() {

            var generator = new DomainPointerSingleLoop();
            var bs = generator.GetBytes();
            var result = BitStringHelper.ByteArrayToString(bs);

            Assert.AreEqual("0000000000010000000000000130096C6F63616C686F7374086C6F63616C6E6574C00C0000010001", result, "result");
            Assert.AreEqual(0xC0, bs[33], "Domain compression pointer");
            Assert.AreEqual(12, bs[34], "Question 0 Offset 0");

        }

        [Test]
        public void TestReadMessage() {

            var generator = new DomainPointerSingleLoop();
            var bs = generator.GetBytes();

            var message = DnsReader.Read(new MemoryStream(bs));
            // DarkDns toolkit is immune to DomainPointerSingleLoop attack
            Assert.AreEqual(1, message.Questions.Count, "count");
            Assert.AreEqual("0.localhost.localnet.0.localhost.localnet.", message.Questions[0].QName.Name, "question0");

        }

    }
}
